home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP11.ZIP / CHAP11 / POLYLINE / IOLEOBJ.CPP < prev    next >
C/C++ Source or Header  |  1993-06-13  |  11KB  |  469 lines

  1. /*
  2.  * IOLEOBJ.CPP
  3.  * Polyline Component Object Chapter 11
  4.  *
  5.  * Implementation of the IOleObject interface for Polyline.  Some of
  6.  * these just pass through to the default handler which does default
  7.  * implementations.
  8.  *
  9.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Software Design Engineer
  12.  * Microsoft Systems Developer Relations
  13.  *
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  16.  */
  17.  
  18.  
  19. #include "polyline.h"
  20.  
  21. //Need this to load strings.
  22. extern HINSTANCE hgInst;
  23.  
  24.  
  25.  
  26. /*
  27.  * CImpIOleObject::CImpIOleObject
  28.  * CImpIOleObject::~CImpIOleObject
  29.  *
  30.  * Parameters (Constructor):
  31.  *  pObj            LPCPolyline of the object we're in.
  32.  *  punkOuter       LPUNKNOWN to which we delegate.
  33.  */
  34.  
  35. CImpIOleObject::CImpIOleObject(LPCPolyline pObj, LPUNKNOWN punkOuter)
  36.     {
  37.     m_cRef=0;
  38.     m_pObj=pObj;
  39.     m_punkOuter=punkOuter;
  40.     return;
  41.     }
  42.  
  43. CImpIOleObject::~CImpIOleObject(void)
  44.     {
  45.     return;
  46.     }
  47.  
  48.  
  49.  
  50. /*
  51.  * CImpIOleObject::QueryInterface
  52.  * CImpIOleObject::AddRef
  53.  * CImpIOleObject::Release
  54.  *
  55.  * Purpose:
  56.  *  IUnknown members for CImpIOleObject object.
  57.  */
  58.  
  59. STDMETHODIMP CImpIOleObject::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  60.     {
  61.     return m_punkOuter->QueryInterface(riid, ppv);
  62.     }
  63.  
  64.  
  65. STDMETHODIMP_(ULONG) CImpIOleObject::AddRef(void)
  66.     {
  67.     ++m_cRef;
  68.     return m_punkOuter->AddRef();
  69.     }
  70.  
  71. STDMETHODIMP_(ULONG) CImpIOleObject::Release(void)
  72.     {
  73.     --m_cRef;
  74.     return m_punkOuter->Release();
  75.     }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. /*
  82.  * CImpIOleObject::SetClientSite
  83.  * CImpIOleObject::GetClientSite
  84.  *
  85.  * Purpose:
  86.  *  Manages the IOleClientSite pointer of our container.
  87.  */
  88.  
  89. STDMETHODIMP CImpIOleObject::SetClientSite(LPOLECLIENTSITE pIOleClientSite)
  90.     {
  91.     if (NULL!=m_pObj->m_pIOleClientSite)
  92.         m_pObj->m_pIOleClientSite->Release();
  93.  
  94.     m_pObj->m_pIOleClientSite=pIOleClientSite;
  95.     m_pObj->m_pIOleClientSite->AddRef();
  96.  
  97.     return NOERROR;
  98.     }
  99.  
  100. STDMETHODIMP CImpIOleObject::GetClientSite(LPOLECLIENTSITE FAR * ppSite)
  101.     {
  102.     //Be sure to AddRef the new pointer you are giving away.
  103.     *ppSite=m_pObj->m_pIOleClientSite;
  104.     m_pObj->m_pIOleClientSite->AddRef();
  105.  
  106.     return NOERROR;
  107.     }
  108.  
  109.  
  110.  
  111.  
  112.  
  113. /*
  114.  * CImpIOleObject::SetHostNames
  115.  *
  116.  * Purpose:
  117.  *  Provides the object with names of the container application and the
  118.  *  object in the container to use in object user interface.
  119.  *
  120.  * Parameters:
  121.  *  pszApp          LPCSTR of the container application.
  122.  *  pszObj          LPCSTR of some name that is useful in window titles.
  123.  *
  124.  * Return Value:
  125.  *  HRESULT         NOERROR
  126.  */
  127.  
  128. STDMETHODIMP CImpIOleObject::SetHostNames(LPCSTR pszApp, LPCSTR pszObj)
  129.     {
  130.     if (NULL!=m_pObj->m_hDlg)
  131.         {
  132.         char        szTemp[128];
  133.  
  134.         wsprintf(szTemp, SZPOLYFRAMETITLE, pszObj);
  135.         SetWindowText(m_pObj->m_hDlg, szTemp);
  136.         }
  137.  
  138.     return NOERROR;
  139.     }
  140.  
  141.  
  142.  
  143.  
  144.  
  145. /*
  146.  * CImpIOleObject::Close
  147.  *
  148.  * Purpose:
  149.  *  Forces the object to close down its user interface and unload.
  150.  *
  151.  * Parameters:
  152.  *  dwSaveOption    DWORD describing the circumstances under which the
  153.  *                  object is being saved and closed.
  154.  *
  155.  * Return Value:
  156.  *  HRESULT         NOERROR if successful, error code otherwise.
  157.  */
  158.  
  159. STDMETHODIMP CImpIOleObject::Close(DWORD dwSaveOption)
  160.     {
  161.     HWND        hWnd;
  162.     BOOL        fSave=FALSE;
  163.  
  164.     hWnd=m_pObj->m_hDlg;
  165.  
  166.     //If object is dirty and we're asked to save, save it and close.
  167.     if (OLECLOSE_SAVEIFDIRTY==dwSaveOption && m_pObj->m_fDirty)
  168.         fSave=TRUE;
  169.  
  170.     /*
  171.      * If asked to prompt, only do so if dirty, then if we get a YES,
  172.      * save as usual and close.  On NO, just close.  On CANCEL return
  173.      * OLE_E_PROMPTSAVECANCELLED.
  174.      */
  175.     if (OLECLOSE_PROMPTSAVE==dwSaveOption && m_pObj->m_fDirty)
  176.         {
  177.         char        szTemp[80];
  178.         char        szTitle[20];
  179.         UINT        uRet;
  180.  
  181.         LoadString(hgInst, IDS_CLOSECAPTION, szTitle, sizeof(szTitle));
  182.         LoadString(hgInst, IDS_CLOSEPROMPT, szTemp, sizeof(szTemp));
  183.         MessageBox(hWnd, szTemp, szTitle, MB_YESNOCANCEL);
  184.  
  185.         if (IDCANCEL==uRet)
  186.             return ResultFromScode(OLE_E_PROMPTSAVECANCELLED);
  187.  
  188.         if (IDYES==uRet)
  189.             fSave=TRUE;
  190.         }
  191.  
  192.     if (fSave)
  193.         {
  194.         m_pObj->SendAdvise(OBJECTCODE_SAVEOBJECT);
  195.         m_pObj->SendAdvise(OBJECTCODE_SAVED);
  196.         }
  197.  
  198.     //We get directly here on OLECLOSE_NOSAVE.
  199.     if (NULL!=hWnd)
  200.         PostMessage(hWnd, WM_CLOSE, 0, 0L);
  201.  
  202.     return NOERROR;
  203.     }
  204.  
  205.  
  206.  
  207.  
  208. /*
  209.  * CImpIOleObject::DoVerb
  210.  *
  211.  * Purpose:
  212.  *  Executes an object-defined action.
  213.  *
  214.  * Parameters:
  215.  *  iVerb           LONG index of the verb to execute.
  216.  *  pMSG            LPMSG describing the event causing the activation.
  217.  *  pActiveSite     LPOLECLIENTSITE to the site involved.
  218.  *  lIndex          LONG the piece on which execution is happening.
  219.  *  hWndParent      HWND of the window in which the object can play in-place.
  220.  *  pRectPos        LPRECT of the object in hWndParent where the object
  221.  *                  can play in-place if desired.
  222.  *
  223.  * Return Value:
  224.  *  HRESULT         NOERROR if successful, error code otherwise.
  225.  */
  226.  
  227. STDMETHODIMP CImpIOleObject::DoVerb(LONG iVerb, LPMSG pMSG
  228.     , LPOLECLIENTSITE pActiveSite, LONG lIndex, HWND hWndParent
  229.     , LPCRECT pRectPos)
  230.     {
  231.     switch (iVerb)
  232.         {
  233.         case OLEIVERB_HIDE:
  234.             if (NULL!=m_pObj->m_hDlg)
  235.                 ShowWindow(m_pObj->m_hDlg, SW_HIDE);
  236.             break;
  237.  
  238.         case OLEIVERB_PRIMARY:
  239.         case OLEIVERB_OPEN:
  240.         case OLEIVERB_SHOW:
  241.             //If we already have a window, just make sure it's showing
  242.             if (NULL!=m_pObj->m_hDlg)
  243.                 {
  244.                 ShowWindow(m_pObj->m_hDlg, SW_NORMAL);
  245.                 SetFocus(m_pObj->m_hDlg);
  246.                 return NOERROR;
  247.                 }
  248.  
  249.             //This stores the dialog handle in m_pObj.
  250.             DialogBoxParam(hgInst, MAKEINTRESOURCE(IDD_EDITDIALOG)
  251.                 , hWndParent, PolyDlgProc, (LPARAM)m_pObj);
  252.             break;
  253.  
  254.         default:
  255.             return ResultFromScode(OLEOBJ_S_INVALIDVERB);
  256.         }
  257.  
  258.     return NOERROR;
  259.     }
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266. /*
  267.  * CImpIOleObject::GetUserClassID
  268.  *
  269.  * Purpose:
  270.  *  Used for linked objects, this returns the class ID of what end
  271.  *  users think they are editing.
  272.  *
  273.  * Parameters:
  274.  *  pClsID          LPCLSID in which to store the CLSID.
  275.  *
  276.  * Return Value:
  277.  *  HRESULT         NOERROR if successful, error code otherwise.
  278.  */
  279.  
  280. STDMETHODIMP CImpIOleObject::GetUserClassID(LPCLSID pClsID)
  281.     {
  282.     /*
  283.      * If you are not registered to handle data other than yourself,
  284.      * then you can just return your class ID here.  If you are registered
  285.      * as usable from Treat-As dialogs, then you need to return the
  286.      * CLSID of what you are really editing.
  287.      */
  288.  
  289.     *pClsID=CLSID_Polyline11;
  290.     return NOERROR;
  291.     }
  292.  
  293.  
  294.  
  295.  
  296.  
  297. /*
  298.  * CImpIOleObject::SetExtent
  299.  *
  300.  * Purpose:
  301.  *  Sets the size of the object in HIMETRIC units.
  302.  *
  303.  * Parameters:
  304.  *  dwAspect        DWORD of the aspect affected.
  305.  *  pszl            LPSIZEL containing the new size.
  306.  *
  307.  * Return Value:
  308.  *  HRESULT         NOERROR if successful, error code otherwise.
  309.  */
  310.  
  311. STDMETHODIMP CImpIOleObject::SetExtent(DWORD dwAspect, LPSIZEL pszl)
  312.     {
  313.     RECT            rc;
  314.  
  315.     if (!(DVASPECT_CONTENT & dwAspect))
  316.         return ResultFromScode(E_FAIL);
  317.  
  318.     SetRect(&rc, 0, 0, (int)pszl->cx, (int)pszl->cy);
  319.     m_pObj->RectConvertMappings(&rc, TRUE);
  320.     m_pObj->m_pIPolyline->SizeSet(&rc, TRUE);
  321.  
  322.     return NOERROR;
  323.     }
  324.  
  325.  
  326.  
  327.  
  328.  
  329. /*
  330.  * CImpIOleObject::GetExtent
  331.  *
  332.  * Purpose:
  333.  *  Retrieves the size of the object in HIMETRIC units.
  334.  *
  335.  * Parameters:
  336.  *  dwAspect        DWORD of the aspect requested
  337.  *  pszl            LPSIZEL into which to store the size.
  338.  *
  339.  * Return Value:
  340.  *  HRESULT         NOERROR if successful, error code otherwise.
  341.  */
  342.  
  343. STDMETHODIM